const { getAll, add, update, remove, getOne } = require('../../utils/dbTools');
const { arrToTree, isValidator2, hasChildNodes } = require('../../utils/util');

const tableName = 'classify';


/**
 * 根据pid计算level
 * 
*/
const computedClassifyLevel = async (pid) => {
    const result = await getOne(tableName, { id: pid });
    if (!result) {
        return false;
    }
    return result.level + 1;
}

/**
 * 获取所有分类
 * 
*/
const classifyList = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { istree } = req.query;
    const result = await getAll(tableName);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    output.data = istree == 1 ? arrToTree(result) : result
    res.send(output);
}

/**
 * 新增分类
 * 
*/
const classifyAdd = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    // 获取登录账号id
    const { id: author_id, bus_id } = req.adminLoginInfo
    const { pid, name, cover } = req.body;
    if (!pid || !name || !cover) {
        output.code = 500;
        output.msg = '参数错误';
        return res.send(output);
    }
    // 检测类目名称是否可用
    const valid = await isValidator2(tableName, { name });
    if (!valid) {
        output.code = 500;
        output.msg = '数据已存在,不能重复添加';
        return res.send(output);
    }
    const data = { pid, name, cover, author_id, bus_id };
    // 通过pid计算level
    data.level = await computedClassifyLevel(pid);
    const result = await add(data, tableName);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    res.send(output);
}

/**
 * 更新分类
 * 
*/
const classifyUpdate = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { id, name, pid, cover } = req.body;
    if (!id) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    const data = {}
    if (name) data.name = name;
    if (cover) data.cover = cover;
    if (pid) {
        data.pid = pid;
        // 如果提交了pid, 则通过pid计算level
        data.level = await computedClassifyLevel(pid);
    }
    const result = await update(id, data, tableName);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    res.send(output);
}

/**
 * 删除分类
*/

const classifyRemove = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { id } = req.query;
    if (!id) {
        output.code = 500;
        output.msg = "参数错误";
        return res.send(output);
    }

    // 检测待删除节点是否存在子节点, 如果存在, 则不允许删除
    const hasChild = await hasChildNodes(tableName, id);
    if (hasChild) {
        output.code = 500;
        output.msg = '该节点下存在子节点, 请先删除子节点';
        return res.send(output);
    }

    const result = await remove(id, tableName);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    res.send(output);
}

module.exports = {
    classifyAdd,
    classifyRemove,
    classifyUpdate,
    classifyList
}
